package com.ld.config;

import lombok.SneakyThrows;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index.html")
                .setViewName("home");
        registry.addViewController("/")
                .setViewName("home");
        registry.addViewController("/home")
                .setViewName("home");
        registry.addViewController("/account/changepwd")
                .setViewName("/account/changepwd");
        registry.addViewController("/account/staffinfo")
                .setViewName("/account/staffinfo");
        registry.addViewController("/account/uploadheadphoto")
                .setViewName("/account/uploadheadphoto");
    }

    /**
     * 配置虚拟路径,为了使用同级目录user_files中的图片
     * @param registry
     */
    @SneakyThrows
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //输出 /D:/IdeaProjects/SpringBoot/user_files/
        String realPath = ResourceUtils.getURL("user_files").getPath();

        registry.addResourceHandler("/user_files/**")
                .addResourceLocations("file:"+realPath);
        //直接访问http://localhost:8888/mysql_sakila/user_files/图片名.jpg 即可显示图片
        //注意 user_files目录是和mysql_sakila项目同级
    }
}
